import request from 'umi-request';
import { getStorageItem, setStorageItem } from './Storage';

const PROJECT_LIST = 'projectList';

const updateProjectList = async () => {
  try {
    const response = await request.get('/api/dict/list/project');
    setStorageItem(PROJECT_LIST, response.object);
    global.console.log('project list is loaded');
  } catch (error) {
    global.console.error('Failed to update project list:', error);
  }
};

const getProjectList = () => getStorageItem(PROJECT_LIST) || [];

const getProjectMap = () => new Map(getStorageItem(PROJECT_LIST)
  .map((item) => [item.key, item.value])) || new Map();

export { updateProjectList, getProjectList, getProjectMap };
